﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PSI.Helper {
    public static class ListHelper {
        public static string CombineWithComma(this List<string> list) {
            var sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++) {
                if (i > 0) {
                    sb.Append(", ");
                }

                sb.Append(list[i]);
            }
            return sb.ToString();
        }

        public static IEnumerable<TSource> DistinctBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element))) { yield return element; }
            }
        }
    }
}